//============================================================================
// Name        : DiscoRefinado.cpp
// Author      : MC e Baggio
// Version     :
// Copyright   : 
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <ctime>
#include <cstdlib>

#include "URLDiskClassP.h"
#include "URL.h"


void writeBloco(void){
	
	FILE *pBloco;
	
	struct _t_URL{
		char t_path[230];
		char t_dominio[60];
		char t_protocol[10];
	} *URLBuffer_T;	
	
	time_t tempo_now, tempo_dif;
	
	pBloco = fopen64 ("DiscoBloco.bin","ab+");
	
	if(pBloco == NULL){ cout << "Erro a abrir o arquivo.... " << endl; exit(1);}
	
	URLBuffer_T = new _t_URL[8000];
	
	URL t = URL("www.glosd2344b3342342342o.com/", "asdjhak342s234hdkasd/kjhaksdj/hakjshdkahsd/", "http://");
	
	string _path;
	string _dominio;
	string _protocolo;
	int res;
	uint numMeta = 8000;
	
	for(uint passo = 1; passo <= numMeta; passo++){
		
		cout << passo << "\t"; 
		time(&tempo_now);
		//Grava
		for(uint id_bloco = 0; id_bloco < numMeta; id_bloco+=passo){
			
			//carrega URLs no Bloco
			for(uint j=0; j<passo; j++){
				_path = (t.get_path().size() >= 230) ? t.get_path().substr(0,229) : t.get_path();
				_dominio = (t.get_dominio().size() >= 60) ? t.get_dominio().substr(0,59) : t.get_dominio();
				_protocolo = (t.get_protocol().size() >= 10) ? t.get_protocol().substr(0,9) : t.get_protocol();
				strcpy(URLBuffer_T[j].t_path, _path.c_str());
				strcpy(URLBuffer_T[j].t_dominio, _dominio.c_str());
				strcpy(URLBuffer_T[j].t_protocol, _protocolo.c_str());
			}
			
			//Grava Bloco
			res = fwrite(URLBuffer_T, sizeof(_t_URL), passo, pBloco );
			if(res != passo){ cout << "Erro ao gravar em writeBloco() " <<  endl; exit(1); }
			
		}
		time(&tempo_dif);
		tempo_dif = tempo_dif - tempo_now;
		cout << tempo_dif << endl;
	}
	
	
	fclose(pBloco);
	delete [] URLBuffer_T;
}


using namespace std;

int main() {
	
	
	writeBloco();
	return 0;
	
	
//	URLDiskClassP discoUrl;	
//	URL tempURL;
//
//	cout << "Grava no disco... " << endl;
//
//	tempURL = URL("www.glosd2344b3342342342o.com/", "asdjhak342s234hdkasd/kjhaksdj/hakjshdkahsd/", "http://");
//
//	long long tam, temp;
//	tam = temp = 0;
//	
//	cout << "Rodando... " << endl;
//	while(true){
//		discoUrl.writeDisk(tempURL);
//		tam = discoUrl.size();
////		cout << "Tamanho do disco: "<<  tam << endl;
//		if(tam > temp) temp = tam;
//		else break;
//	}	
//	cout << "Limite em: " << tam << endl;
//	
//	return 0;
}
